# Copyright (C) 2015  ABRT Team
# Copyright (C) 2015  Red Hat, Inc.
#
# This file is part of faf.
#
# faf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# faf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with faf.  If not, see <http://www.gnu.org/licenses/>.


"""
Drop reportunknownpackage.running fields remove installed prefix

Revision ID: 1c7edfbf8941
Revises: 43bd2d59838e
Create Date: 2015-03-18 15:19:28.412310
"""

from alembic.op import (create_foreign_key, create_unique_constraint, execute,
                        drop_constraint, drop_column, alter_column, add_column)
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '1c7edfbf8941'
down_revision = '43bd2d59838e'


t = "reportunknownpackages"


def upgrade() -> None:
    # constraint name is autogenerated differently between pg versions
    try:
        drop_constraint("reportunknownpackages_report_id_key", t)
    except: # pylint: disable=bare-except
        execute('ROLLBACK')
        drop_constraint(
            "reportunknownpackages_report_id_type_name_installed_epoch_i_key",
            t)

    drop_constraint("reportunknownpackages_installed_arch_id_fkey", t)

    drop_column(t, "running_epoch")
    drop_column(t, "running_version")
    drop_column(t, "running_release")
    drop_column(t, "running_arch_id")

    alter_column(t, "installed_epoch", new_column_name="epoch")
    alter_column(t, "installed_version", new_column_name="version")
    alter_column(t, "installed_release", new_column_name="release")
    alter_column(t, "installed_arch_id", new_column_name="arch_id")

    create_foreign_key("reportunknownpackages_arch_id_fkey", t,
                       "archs", ["arch_id"], ["id"])

    fields = ["report_id", "type", "name", "epoch",
              "version", "release", "arch_id"]

    create_unique_constraint("reportunknownpackages_report_id_key", t, fields)


def downgrade() -> None:
    drop_constraint("reportunknownpackages_report_id_key", t)
    drop_constraint("reportunknownpackages_arch_id_fkey", t)

    add_column(t, sa.Column('running_epoch', sa.Integer(),
                            nullable=True))
    add_column(t, sa.Column('running_version', sa.String(64),
                            nullable=True))
    add_column(t, sa.Column('running_release', sa.String(64),
                            nullable=True))
    add_column(t, sa.Column('running_arch_id', sa.Integer(),
                            sa.ForeignKey('archs.id'),
                            nullable=True))

    alter_column(t, "epoch", new_column_name="installed_epoch")
    alter_column(t, "version", new_column_name="installed_version")
    alter_column(t, "release", new_column_name="installed_release")
    alter_column(t, "arch_id", new_column_name="installed_arch_id")

    fields = ["report_id", "type", "name", "installed_epoch",
              "installed_version", "installed_release", "installed_arch_id",
              "running_epoch", "running_version", "running_release",
              "running_arch_id"]

    create_unique_constraint("reportunknownpackages_report_id_key", t, fields)

    create_foreign_key("reportunknownpackages_installed_arch_id_fkey", t,
                       "archs", ["installed_arch_id"], ["id"])
